import { Tray } from "electron";
import { EventClass } from "@lib/alen-helper/base/baseClass";
import { AppMenu } from "./menu";

export namespace AppTray {
    export interface Options {
        icon: string,
        toolTip: string | null,
        menu: AppMenu | null,
    }
}

export class AppTray extends EventClass<{
    click: null,
}> {
    _srcTray: Tray;

    constructor(
        options: Partial<AppTray.Options>,
        BaseOptions?: Partial<EventClass.BaseOptions>
    ) {
        super('AppTray', BaseOptions);
        if (!options.icon) throw this._err('参数出错', '没有图标');
        this._srcTray = this._buildSrcTray(options.icon);
        this._srcTray.addListener('click', () => {
            this.triggerEvent('click', null);
        });
        this._setOptions(options, false);
    }

    private _buildSrcTray(icon: string) {
        return new Tray(icon);
    }

    private _setOptions(options: Partial<AppTray.Options>, setIcon: boolean = true) {
        if (setIcon && options.icon) this.setIcon(options.icon);
        if (typeof options.toolTip !== 'undefined') this.setToolTip(options.toolTip);
        if (typeof options.menu !== 'undefined') this.setMenu(options.menu);
    }

    setOptions(options: Partial<AppTray.Options>) {
        this._setOptions(options);
    }

    setIcon(icon: string) {
        this._srcTray.setImage(icon);
    }

    setToolTip(txt: string | null) {
        this._srcTray.setToolTip(txt ? txt : '');
    }

    setMenu(menu: AppMenu | null) {
        this._srcTray.setContextMenu(menu ? menu._srcMenu : null);
    }

    destroy() {
        this._srcTray.destroy();
    }

}